﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using C.Entities;
using C.Unite;
using ChanExt;
using MySoft.Data;
using Newtonsoft.Json.Linq;

namespace ArtLand.OA.Project.ADM
{
    public partial class Adm_BUDGETList : CommonPageBase
    {
        #region Inti
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //Grid1.AllowCellEditing = false;
                Drp_YERNUB.Items.Add(new ListItem((DateTime.Now.Year - 1).ToString() + "年", (DateTime.Now.Year - 1).ToString()));
                Drp_YERNUB.Items.Add(new ListItem((DateTime.Now.Year).ToString() + "年", (DateTime.Now.Year).ToString()));
                Drp_YERNUB.SelectedValue = DateTime.Now.Year.ToString();

                FormInfo.DataBaseBind("1-1", Drp_BDTYPE);


                JObject defaultObj = new JObject();
                defaultObj.Add("YERNUB", DateTime.Now.Year);
                defaultObj.Add("BDTYPE", "");
                defaultObj.Add("AMOUNT", 0);
                defaultObj.Add("ReMark", "");
                btnNew.OnClientClick = Grid1.GetAddNewRecordReference(defaultObj, false);

                BindGrid(GetWhere());
            }
        }
        #endregion

        #region BindGrid
        private void BindGrid(WhereClip wc)
        {
            Grid1.RecordCount = TbsDbSession.Default.From<Adm_BUDGET>().Where(wc).Count();

            List<Adm_BUDGET> items = TbsDbSession.Default.From<Adm_BUDGET>().Where(wc).OrderBy(Adm_BUDGET._.ID.Desc).Page(Grid1.PageSize, (Grid1.PageIndex + 1)).ToList();

            // 3.绑定到Grid
            Grid1.DataSource = items;
            Grid1.DataBind();
        }



        protected void Grid1_PageIndexChange(object sender, ChanExt.GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid(GetWhere());
        }


        #endregion

        #region Events

        public WhereClip GetWhere()
        {
            WhereClip wc = WhereClip.All;
            //if (!String.IsNullOrEmpty(ttbxMyBox2.Text))
            //{
            //    wc = wc && (Adm_BUDGET._.YERNUB.Contain(ttbxMyBox2.Text) || V_EmpInfo._.EMPLID.Contain(ttbxMyBox2.Text));
            //}
            return wc;
        }



        #region 搜索框
        //// 点击 TwinTriggerBox 的搜索按钮
        //protected void ttbxMyBox2_Trigger2Click(object sender, EventArgs e)
        //{
        //    if (!String.IsNullOrEmpty(ttbxMyBox2.Text))
        //    {
        //        // 执行搜索动作
        //        BindGrid(GetWhere());
        //        ttbxMyBox2.ShowTrigger1 = true;
        //    }
        //    else
        //    {
        //        Alert.ShowInTop("请输入你要搜索的关键词！");
        //    }
        //}

        //// 点击 TwinTriggerBox 的取消按钮
        //protected void ttbxMyBox2_Trigger1Click(object sender, EventArgs e)
        //{
        //    // 执行清空动作

        //    ttbxMyBox2.Text = "";
        //    ttbxMyBox2.ShowTrigger1 = false;
        //    BindGrid(GetWhere());
        //} 
        #endregion


        #endregion

        protected void Grid1_RowCommand(object sender, ChanExt.GridCommandEventArgs e)
        {
            int id = int.Parse(Grid1.DataKeys[e.RowIndex][0].ToString());
            if (e.CommandName == "Del")
            {
                TbsDbSession.Default.Delete<Adm_BUDGET>(Adm_BUDGET._.ID == id);
                Alert.ShowInTop("删除成功！");
                BindGrid(GetWhere());
            }
        }

        protected void Grid1_RowDataBound(object sender, ChanExt.GridRowEventArgs e)
        {
            int year = int.Parse(e.Values[0].ToString());
            decimal a = decimal.Parse(e.Values[2].ToString());
            object admamount = TbsDbSession.Default.From<Adm_EXPNSE>().Where(Adm_EXPNSE._.CHKBUG == true && Adm_EXPNSE._.STATUS == 1 && Adm_EXPNSE._.PLDATE.Year == year).Select(Adm_EXPNSE._.AMOUNT.Sum()).ToScalar();
            if (admamount.ToString() == "")
                admamount = "0";
            decimal c = a - decimal.Parse(admamount.ToString());
            string title = year.ToString() + "年费用申请列表";
            if (admamount.ToString() != "0")
                e.Values[3] = "<a href=\"javascript:;\" onclick=\"top.addUTab('/ADM/Adm_ExpnseList.aspx?y=" + year + "', 'expn', '" + title + "', '');\">" + c.ToString() + "</a>";
            else
                e.Values[3] = c.ToString();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            int returnvalue = 0;
            Dictionary<int, Dictionary<string, string>> modifiedDict = Grid1.GetModifiedDict();
            foreach (int rowIndex in modifiedDict.Keys)
            {
                int rowID = Convert.ToInt32(Grid1.DataKeys[rowIndex][0]);
                UpdateDataRow(modifiedDict[rowIndex], rowID);
            }
            returnvalue += modifiedDict.Keys.Count();
            // 新增数据
            List<Dictionary<string, string>> newAddedList = Grid1.GetNewAddedList();
            for (int i = newAddedList.Count - 1; i >= 0; i--)
            {
                UpdateDataRow(newAddedList[i], 0);
            }
            returnvalue += newAddedList.Count;
            if (returnvalue > 0)
            {
                BindGrid(GetWhere());
                Alert.Show("保存成功！");
            }
            else
                Alert.Show("您未对任何行的数据进行修改！");
        }


        private static void UpdateDataRow(Dictionary<string, string> rowDict, int Dtype)
        {
            Adm_BUDGET bud = new Adm_BUDGET();
            if (Dtype == 0)
            {
                bud.DeAttach();
            }
            else
            {
                bud.ID = Dtype;
                bud.Attach();
            }
            if (rowDict.ContainsKey("AMOUNT"))
            {
                bud.AMOUNT = decimal.Parse(rowDict["AMOUNT"]);
            }
            if (rowDict.ContainsKey("BDTYPE"))
            {
                bud.BDTYPE = rowDict["BDTYPE"];
            }
            if (rowDict.ContainsKey("YERNUB"))
            {
                bud.YERNUB = int.Parse(rowDict["YERNUB"]);
            }
            if (rowDict.ContainsKey("REMARK"))
            {
                bud.REMARK = rowDict["REMARK"];
            }
            if (Dtype == 0)
            {
                TbsDbSession.Default.Insert<Adm_BUDGET>(bud);
            }
            else
            {
                TbsDbSession.Default.Update<Adm_BUDGET>(bud);
            }
        }



    }
}